home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12712 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help with strcmp
  5. Date: 2 Apr 1996 12:39:21 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4jr75p$onk@sparcserver.lrz-muenchen.de>
  9. References: <4jpiek$lp6@blaze.cs.jhu.edu> <316196CB.1125@diagram.fr>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. Jean-Dominique BOISSE <jdbo@diagram.fr> writes:
  13.  
  14. >>         if ( strcasecmp(fp, "<action>\n") == 0)  /* the coredump is here */
  15.  
  16. >    I think that you must use strncasecmp instead of strcasecmp, like this
  17.  
  18. >        strncasecmp(fp,"<action>\n",40)
  19.  
  20. >    This function will only read 40 characters from fp for the test.
  21.  
  22. >    You can also put a '\0' at the very end of fp (fp[40])  and still use     
  23. >    strcasecmp, but read only 39 chars from handle.
  24.  
  25. fgets() always reads a C string, even if it does not read a whole line
  26. in some cases.
  27.  
  28. We cannot say much about the behaviour of strcasecmp() and strncasecmp()
  29. on a given machine, but if they are just implementations of strcmp() and
  30. strncmp() that ignore differences in case when comparing upper and lower
  31. case letters, this is nonsense.
  32.  
  33. strcmp() and strncmp() are designed to handle C strings, so they know how 
  34. to treat '\0' in a string. Otherwise
  35.  
  36.    int STRCMP(const char *s1, const char *s2)
  37.    {
  38.       size_t sz1 = strlen(s1), sz2 = strlen(s2);
  39.       return strncpm(s1, s2, sz1 < sz2 ? sz1 : sz2);
  40.    }
  41.  
  42. would be a common replacement for the then useless strcmp() function.
  43.  
  44. Kurt
  45. --
  46. | Kurt Watzka                             Phone : +49-89-2180-6254
  47. | watzka@stat.uni-muenchen.de
  48.